// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Text;
using System.Xml;

namespace QuoteAdvantageEditor
{
	/// <summary>
	/// Summary description for Quote.
	/// </summary>
   public class Quote 
   {
      private Guid m_QuoteId;
      private string m_strQuoteNumber;
      private string m_strName;
      private string m_strDescription;
      private ArrayList m_LineItems = new ArrayList();

      public Quote()
      {
			
      }

      public ArrayList LineItems
      {
         get { return m_LineItems; }
      }

      public string Description
      {
         get { return m_strDescription; }
         set { m_strDescription = value; }
      }

      public string Name
      {                                                    
         get { return m_strName; }
         set { m_strName = value; }
      }

      public Guid QuoteId
      {
         get { return m_QuoteId;}
         set { m_QuoteId = value;}
      }

      public string QuoteNumber
      {
         get { return m_strQuoteNumber; }
         set { m_strQuoteNumber = value; }
      }

       public decimal CalculatePrice()
      {
         return new decimal(0.0);   
      }

      // Retrieve all items on this quote that are root level, configurable items
      public ArrayList GetConfigurableItems()
      {
         ArrayList   resultList = new ArrayList(10);

         foreach (QuoteDetail item in this.LineItems)
         {
            if (item.ConfigParentId == Guid.Empty)
            {
               resultList.Add(item);
            }                       
         }
         
         return resultList;   
      }

      public QuoteDetail FindQuoteDetail(Guid guidQuoteDetailId)
      {
         QuoteDetail    objResultItem = null;
     
         foreach (QuoteDetail objItem in this.LineItems)
         {
            if (objItem.QuoteDetailId == guidQuoteDetailId)
            {   
               objResultItem = objItem;
               break;
            }
         }

         return objResultItem;
      }

      public QuoteDetail FindQuoteDetail(string strProductNumber, string strCategoryId)
      {
         QuoteDetail    objResultItem = null;
     
         foreach (QuoteDetail objItem in this.LineItems)
         {
            if (objItem.ProductNumber == strProductNumber && objItem.CategoryId == strCategoryId)
            {   
               objResultItem = objItem;
               break;
            }
         }

         return objResultItem;
      }


      // Search an item's quote details for a line item with a specific category id
      public QuoteDetail FindCategoryItem(QuoteDetail objParentItem, string strCategoryId)
      {
         QuoteDetail    objResultItem = null;
     
         foreach (QuoteDetail objItem in this.LineItems)
         {
            if (objItem.ConfigParentId == objParentItem.QuoteDetailId)
            {
               if (objItem.CategoryId == strCategoryId)
               {   
                  objResultItem = objItem;
                  break;
               }
            }
         }

         return objResultItem;
      }


      // Search an item's quote details for a line item with a specific category id and a specific Product Number
      public QuoteDetail FindCategoryItem(QuoteDetail objParentItem, string strCategoryId, string strProductNumber)
      {
         QuoteDetail    objResultItem = null;
     
         foreach (QuoteDetail objItem in this.LineItems)
         {
            if (objItem.ConfigParentId == objParentItem.QuoteDetailId)
            {
               if (objItem.CategoryId == strCategoryId && objItem.ProductNumber == strProductNumber)
               {   
                  objResultItem = objItem;
                  break;
               }
            }
         }

         return objResultItem;
      }

      /*  Sample XML
       * <quote>
            <quoteid>{165EAFD9-E14D-4416-999B-543957649F35}</quoteid>
            <quotenumber>QUO-01004-S331UK</quotenumber>
            <name>Road/Touring clothing</name>
            <description>Solid Bike Parts quote for socks, gloves, and jerseys.</description>
            <totalamount>2280.9317</totalamount>
         </quote>
*/
      public void LoadFromXml(string xmlData)
      {
         XmlDocument    doc = new XmlDocument();

         doc.LoadXml(xmlData);
         XmlNode     nodeRoot = doc.DocumentElement;

         foreach (XmlNode nodeItem in nodeRoot.ChildNodes)
         {      
            switch (nodeItem.Name)
            {
               case "description":
                  this.Description = nodeItem.InnerText;
                  break;
               
               case "name":
                  this.Name = nodeItem.InnerText;
                  break;
               
               case "quoteid":
                  this.QuoteId = new Guid(nodeItem.InnerText);
                  break;

               case "quotenumber":
                  this.QuoteNumber = nodeItem.InnerText;
                  break;    
            }
            
           
         } // end foreach
      }

      /*  Sample XML for quotedetails
      <quotedetails>
         <quotedetail>
            <lineitemnumber>1</lineitemnumber>
            <quantity>35</quantity>
            <productid name="Sport-100 helmet, Blue" dsc="0">
               {C05DBC1C-DC92-431D-88C6-B2046235DD8A}
            </productid>
            <priceperunit>34.99</priceperunit>
            <extendedamount>1318.7028</extendedamount>
            <tax>94.0528</tax>
            <baseamount>1224.65</baseamount>
         </quotedetail>
      </quotedetails>
*/
      public void LoadQuoteDetailsFromXml(string xmlData)
      {
         XmlDocument    doc = new XmlDocument();

         doc.LoadXml(xmlData);
         XmlNode     nodeRoot = doc.DocumentElement;

         this.LineItems.Clear();

         foreach (XmlNode nodeItem in nodeRoot.ChildNodes)
         {
            QuoteDetail objLineItem = new QuoteDetail();
   
            objLineItem.QuoteOwner = this;

            foreach (XmlNode nodeProperty in nodeItem.ChildNodes)
            {
               switch (nodeProperty.Name)
               {            
                  case "qadv_config_parent_id":               
                     objLineItem.ConfigParentId = new Guid(nodeProperty.InnerText);
                     break;    
                  
                  case "qadv_config_category_id":               
                     objLineItem.CategoryId = nodeProperty.InnerText;
                     break;    
          
                  case "extendedamount":
                     objLineItem.ExtendedAmount = Convert.ToDecimal(nodeProperty.InnerText);
                     break;

                  case "lineitemnumber":
                     objLineItem.LineItemNumber = Convert.ToInt32(nodeProperty.InnerText);
                     break;

                  case "priceperunit":
                     objLineItem.PricePerUnit = Convert.ToDecimal(nodeProperty.InnerText);
                     break;

                  case "productdescription":               
                     objLineItem.ProductDescription = nodeProperty.InnerText;
                     break;    

                  case "productid":
                     objLineItem.ProductId = new Guid(nodeProperty.InnerText);    
                     objLineItem.Name = nodeProperty.Attributes["name"].Value.ToString();
                     break;

                  case "quantity":
                     objLineItem.Quantity = Convert.ToDecimal(nodeProperty.InnerText);
                     break;  

                  case "quotedetailid":
                     objLineItem.QuoteDetailId = new Guid(nodeProperty.InnerText);
                     break;

                  case "quoteid":
                     objLineItem.QuoteId = new Guid(nodeProperty.InnerText);
                     break;  

                  case "shipto_line3":
                     objLineItem.ShipToLine3 = nodeProperty.InnerText;
                     break;

                  case "shipto_fax":
                     objLineItem.ShipToFax = nodeProperty.InnerText;
                     break;

                  case "uomid":
                     objLineItem.UnitOfMeasureId = new Guid(nodeProperty.InnerText);
                     break;
               }            
            } // end foreach nodeProperty

            this.LineItems.Add(objLineItem);
         } // end foreach nodeItem

      }

	} // end class
}
